export class Utils {

    static getJsonObject(data) {
        return typeof data === 'object' ? data : JSON.parse(data);
    }

    static getPermissions(model) {
        let permissions: any = {};
        let object = model;
        for (const key in object) {
            if (object.hasOwnProperty(key)) {
                const element = object[key];
                if (typeof element.permission === 'object') {
                    let tabs = Utils.getTabPermission(element.permission);
                    permissions[key] = tabs && tabs.length > 0;
                } else {
                    permissions[key] = Utils.getBasePermission(element.permission);
                }
            }
        }
        return permissions;
    }

    static getMenuList(model) {
        let fundPermissions = Utils.getPermissions(model);
        let menu = [];
        for (const key in model) {
            if (model.hasOwnProperty(key)) {
                if (fundPermissions[key]) {
                    let element = model[key];
                    menu.push({
                        title: element.title,
                        domId: key,
                        detail: element.detail || ''
                    });
                }
            }
        }
        return menu;
    }

    static getItemIds(object) {
        let ids = [];
        for (const key in object) {
            if (object.hasOwnProperty(key)) {
                const element = object[key];
                ids.push(key);
            }
        }
        return ids;
    }

    // 拷贝对象数组
    static copyObjArray(fromItem, toItem) {
        for (let obj of fromItem) {
            var newObj = {};
            for (let key of Object.keys(obj)) {
                newObj[key] = obj[key];
            }
            toItem.push(newObj);
        }
    }

    /**
     * 拷贝对象  将fromObj的属性值copy到toObj中（toObj中没有对应的值才拷贝，有的话就不拷贝）
     * 用于修改和添加对象时候的对象赋值
     *
     */
    static copyObj(toObj, fromObj) {
        console.log(Object.keys(fromObj));
        for (let key of Object.keys(fromObj)) {
            if (fromObj.hasOwnProperty(key) && fromObj[key]) {
                toObj[key] = fromObj[key];
            }
        }
        return toObj;
    }
    /**
     * 拷贝对象  将fromObj的所有属性值copy到toObj中
     *
     */
    static copyObjAll(toObj, fromObj) {
        console.log(Object.keys(fromObj));
        for (let key of Object.keys(fromObj)) {
            toObj[key] = fromObj[key];
        }
        return toObj;
    }

    static simpleDeepClone(initalObj) {
        var obj = {};
        for (var i in initalObj) {
            obj[i] = initalObj[i];
        }
        return obj;
    }

    static deepClone(initalObj) {
        var obj = {};
        for (var i in initalObj) {
            var prop = initalObj[i];
            // 避免相互引用对象导致死循环，如initalObj.a = initalObj的情况
            if (prop === obj) {
                continue;
            }
            if (typeof prop === 'object') {
                obj[i] = (prop.constructor === Array) ? [] : Object.create(prop);
            } else {
                obj[i] = prop;
            }
        }
        return obj;
    }

    /**
     * 数组排序
     *
     */
    static getOrderArray(arr, orderBy, isDesc?) {


        for (var i = 0; i < arr.length; i++) {
            var element = arr[i];
            element[orderBy]
        }
        return arr;
    }

    static insertSort(array, orderBy, isDesc?: boolean) {
        // isDesc默认false 顺序排序, 传入true 时倒序排序
        var i = 1, j, temp, key, len = array.length;
        for (; i < len; i++) {
            temp = j = i; key = array[j];
            while (--j > -1) {
                if (isDesc) {
                    if (array[j][orderBy] < key[orderBy]) {
                        array[j + 1] = array[j];
                    } else {
                        break;
                    }
                } else {
                    if (array[j][orderBy] > key[orderBy]) {
                        array[j + 1] = array[j];
                    } else {
                        break;
                    }
                }
            }
            array[j + 1] = key;
        }
        return array;
    }


    /**
     * 初始化默认可选列
     * @param initChoiceColumnData 初始化可选列中选中数据
     * @param oldDataCoumn 初始所有列
     * @param tmpDataColumn 用于匹配可选列中选中数据
     */
    static initChoiceColumn(initChoiceColumnData: any[], oldDataCoumn: any[], tmpDataColumn: any[]) {
        let count = 0;
        for (let i = 0; i < oldDataCoumn.length; i++) {
            if (oldDataCoumn[i].hidden == false) {
                tmpDataColumn[count] = oldDataCoumn[i];
                oldDataCoumn[i].hidden = '';
                count++;
            }
            initChoiceColumnData.push({ label: oldDataCoumn[i].name, value: oldDataCoumn[i] });
        }
    }

    /**
     * 用于选中/取消可选列使用
     * @param checkBoxSelectData 可选列中选中数据
     * @param oldDataCoumn 初始所有列
     * @param tmpDataColumn 用于匹配可选列中选中数据
     */
    static getTableChoiceColumn(checkBoxSelectData: any[], oldDataCoumn: any[], tmpDataColumn: any[]) {
        for (let i = 0; i < oldDataCoumn.length; i++) {
            oldDataCoumn[i].hidden = true;
        }
        for (var index = 0; index < checkBoxSelectData.length; index++) {
            for (let i = 0; i < oldDataCoumn.length; i++) {
                if (oldDataCoumn[i].field == checkBoxSelectData[index].field) {
                    oldDataCoumn[i].hidden = '';
                }
            }
        }
        tmpDataColumn = [];
        tmpDataColumn = checkBoxSelectData;
    }

    /**
     * tab菜单数组
     * @param permission 
     */
    static getTabPermission(permission: any[]) {
        let tmp: boolean = true;
        let count = 0;
        let otherOperationPermissionArray = JSON.parse(localStorage.getItem('resourcesList'));
        if (tmp) {// 是否打开检查权限
            if (otherOperationPermissionArray !== undefined && otherOperationPermissionArray != null && otherOperationPermissionArray.length > 0 && permission !== undefined) {
                for (let j = 0; j < permission.length; j++) {
                    count = 0;
                    let element = permission[j].code;
                    for (let i = 0; i < otherOperationPermissionArray.length; i++) {
                        let tmpValue = otherOperationPermissionArray[i] + '';
                        tmpValue = tmpValue.substring(tmpValue.lastIndexOf('.') + 1, tmpValue.length);
                        if (element === tmpValue) {
                            count++;
                            break;
                        }
                    }
                    if (count === 0) {
                        permission.splice(j, 1);
                        j--;
                    }
                }
                return permission;
            } else {
                return permission = [];
            }
        } else {
            return permission;
        }
    }

    static getBasePermission(otherPermission: any) {
        let tmp: boolean = true;
        let otherOperationPermissionArray = JSON.parse(localStorage.getItem('resourcesList'));
        if (tmp) {// 是否打开检查权限
            if (otherOperationPermissionArray !== undefined && otherOperationPermissionArray != null && otherOperationPermissionArray.length > 0) {
                for (let i = 0; i < otherOperationPermissionArray.length; i++) {
                    if (otherPermission === otherOperationPermissionArray[i]) {
                        return true;
                    }
                }
            } else {
                return false;
            }
        } else {
            return true;
        }
    }
    /**
         * 
         * @param date 日期格式化
         */
    static dateFormat(date) {
        if (date != undefined && date != 'undefined') {
            var dateTmp: any = new Date(date);
            return dateTmp.format('yyyy-MM-dd hh:mm:ss');
        } else {
            return '';
        }

    }

    /**
     * 去重 return new bArr
     * @param aArr
     * @param bArr
     * @param checkBy 
     */
    static checkOutRepeat(aArr, bArr, checkBy) {
        let tempArr = [];
        for (let i = 0; i < aArr.length; i++) {
            const elementA = aArr[i];
            for (let j = 0; j < bArr.length; j++) {
                const elementB = bArr[j];
                if (elementA[checkBy] == elementB[checkBy]) {
                    bArr.splice(j, 1);
                }
            }
        }
        return bArr;
    }

    /**
     * 自定义表格外部查询按钮，清空表格排序，过滤，还原过滤列
     * @param tableHeadArr 
     */
    static cleanTableParams(tableHeadArr) {
        if (tableHeadArr && tableHeadArr.length > 0) {
            for (var i = 0; i < tableHeadArr.length; i++) {
                if (tableHeadArr[i].sort) tableHeadArr[i].sort = '';
                if (tableHeadArr[i].searchInput) tableHeadArr[i].searchInput = '';
            }
        }
        return tableHeadArr;
    }

    /**
     * 把树型数据的node组成一个数组
     * @param node
     */
    static getTreeNodeArr(node, tempArr) {
        tempArr.push(node);
        if (node.children && node.children.length > 0) {
            for (let i = 0; i < node.children.length; i++) {
                const element = node.children[i];
                this.getTreeNodeArr(element, tempArr)
            }
        }
        return tempArr;
    }

    /**
     * 设定画布高度
     * @memberof Utils
     */
    static setBodyHeight() {
        // 设定画布高度 headHeight + 80 + contentHeight + 130 + footerHeight
        let head = document.getElementById('page_head');
        let content = document.getElementById('page_content');
        let footer = document.getElementById('page_footer');
        let page = document.getElementById('draw_paper');
        let contentHeight = 0;
        if (content) {
            contentHeight = content.clientHeight > 15000 ? 12000 : content.clientHeight;
        }
        page.style.height = (head ? head.clientHeight : 0) + 80 + contentHeight + 130 + (footer ? footer.clientHeight : 0) + 'px';
        page.style.overflow = 'hidden';

    }
    static resetBodyHeight() {
        let page = document.getElementById('draw_paper');
        page.style.height = 'auto';
        page.style.overflow = 'auto';
    }

    /**
     * 删除数组b有a没有的元素,返回数组b
     * @param {any} a 
     * @param {any} b 
     * @memberof Utils
     */
    static findBInA(a: any[], b: any[], key) {
        let arr = JSON.parse(JSON.stringify(b));
        for (let i = 0; i < b.length; i++) {
            const elementB = b[i];
            let has = false;
            for (let j = 0; j < a.length; j++) {
                const elementA = a[j];
                if (elementB[key] == elementA[key]) {
                    has = true; // 只要含有一个则true
                }
            }
            if (has) {

            } else {
                for (let i = 0; i < arr.length; i++) {
                    const element = arr[i];
                    if (element[key] == elementB[key]) {
                        arr.splice(i, 1);
                    }
                }
            }
        }
        return arr;
    }

}

export default Utils;


